﻿using ClockLib;
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;

namespace ClockServer.Services
{
    public class ClockService : BackgroundService
    {
        private Clock clock;
        private ClockLib.Logger.ILogger? logger=null;
        public ClockService(Clock clock)
        {
            this.clock = clock;
        }
        public ClockService(Clock clock, ClockLib.Logger.ILogger logger):this(clock)
        {
            this.logger= logger; 
        }
        public override Task StartAsync(CancellationToken cancellationToken)
        {
            logger?.Debug("Clock service start...");
            return base.StartAsync(cancellationToken);
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            logger?.Debug("Clock is running...");
            await clock.RunAsync();
        }

        public override Task StopAsync(CancellationToken cancellationToken)
        {
            logger?.Debug("Clock stopped.");
            return base.StopAsync(cancellationToken);
        }
    }
}
